package com.morn.weibo.fragment;

import java.util.List;

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.morn.weibo.MessageActivity;
import com.morn.weibo.NewMessageActivity;
import com.morn.weibo.R;
import com.morn.weibo.WeiboApplication;
import com.morn.weibo.adapter.ConversationListAdapter;
import com.morn.weibo.http.Fanfou;
import com.morn.weibo.types.Conversation;
import com.morn.weibo.types.User;

public class ConversationFragment extends ListFragment implements
		LoaderCallbacks<List<Conversation>> {

	private static final String TAG = ConversationFragment.class
			.getSimpleName();

	private ActionMode mActionMode;

	private ConversationListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setHasOptionsMenu(true);

		// new GetConversationTask().execute();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setEmptyText("No applications");
		mAdapter = new ConversationListAdapter(getActivity());
		setListAdapter(mAdapter);
		setListShown(false);
		this.getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.main_action_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_refresh:
			Toast.makeText(getActivity(), "ConversationFragment",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menu_new:
			Intent intent = new Intent(getActivity(), NewMessageActivity.class);
			getActivity().startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if (mActionMode != null) {
			v.setSelected(!v.isSelected());
			return;
		}
		Conversation conversation = (Conversation) getListAdapter().getItem(
				position);
		User user1 = conversation.getMessage().getSender();
		User user2 = conversation.getMessage().getRecipient();
		String otherId = conversation.getOtherid();
		User otherUser = otherId.equals(user1.getId()) ? user1 : user2;
		Intent intent = new Intent(getActivity(), MessageActivity.class);
		intent.putExtra("otherUser", otherUser);
		getActivity().startActivity(intent);
	}

	public static class ConversationLoader extends
			AsyncTaskLoader<List<Conversation>> {

		private Fanfou fanfou;

		public ConversationLoader(Context context) {
			super(context);
			Log.i(TAG, "ConversationLoader create");
			fanfou = WeiboApplication.getFanfou();
		}

		@Override
		protected void onStartLoading() {
			super.onStartLoading();
			Log.i(TAG, "onStartLoading");
			forceLoad();
		}

		@Override
		public List<Conversation> loadInBackground() {
			List<Conversation> temp = null;
			try {
				temp = fanfou.getConversationList(null, null);
				Log.i(TAG, "temp.size()=" + temp.size());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return temp;
		}

		@Override
		public void deliverResult(List<Conversation> data) {
			// TODO Auto-generated method stub
			super.deliverResult(data);
		}

	}

	@Override
	public Loader<List<Conversation>> onCreateLoader(int id, Bundle args) {
		return new ConversationLoader(getActivity());
	}

	@Override
	public void onLoadFinished(Loader<List<Conversation>> loader,
			List<Conversation> data) {
		Log.i(TAG, "onLoadFinished");
		mAdapter.setData(data);
		setListShown(true);
		setListShownNoAnimation(true);
	}

	@Override
	public void onLoaderReset(Loader<List<Conversation>> loader) {
		mAdapter.setData(null);
		Log.i(TAG, "onLoaderReset");

	}

}
